<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '360px'
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler (val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions (data) {
      const category = this.chartData.map(item => this.parseTime(item.date, '{y}-{m}-{d}'));
      const total = this.chartData.map(item => item.total);
      const text = this.chartData.map(item => item.text);
      const video = this.chartData.map(item => item.video);
      const image = this.chartData.map(item => item.image);
      console.log(this.chartData, category, total, text, video, image)
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          top: 10,
          textStyle: {
            color: '#fff'
          },
          data: ['total', 'text', 'image', 'video']
        },
        grid: {
          top: '10%',
          left: '3%',
          right: '5%',
          bottom: '0',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: category,
        }],
        yAxis: [{
          type: 'value',
          splitLine: {     //网格线
            "show": false
          },
          
        }],
        series: [{
          name: 'text',
          type: 'line',
          barWidth: '24',
          itemStyle: {
            color: '#FFC421'
          },
          data: text,
        },{
          name: 'image',
          type: 'line',
          barWidth: '24',
          itemStyle: {
            color: '#F96402'
          },
          data: image,
        }, {
          name: 'total',
          type: 'line',
          barWidth: '24',
          itemStyle: {
            color: '#FF0000'
          },
          data: total,
        }, {
          name: 'video',
          type: 'line',
          barWidth: '24',
          itemStyle: {
            color: '#11EC5E'
          },
          data: video,
        }]
      })
    }
  }
}
</script>
